package com.example.droidwarehouse;

import com.example.droidwarehouse.barcodescanner.IntentIntegrator;
import com.example.droidwarehouse.barcodescanner.IntentResult;
import com.example.droidwarehouse.item.Item;
import com.example.droidwarehouse.item.ItemService;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity{
	private Button b_save;
    private Button b_cancel;
    private EditText e_itemID;
    private EditText e_itemName;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        IntentIntegrator.initiateScan(this);			//Scan Code before Activity shows up
        setContentView(R.layout.register_activity);
        
        e_itemID = (EditText) findViewById(R.id.e_itemID);
        e_itemName = (EditText) findViewById(R.id.e_itemName);
        
        b_cancel = (Button) findViewById(R.id.b_cancel);
        b_cancel.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
                RegisterActivity.this.finish();
            }
        });
        
        b_save = (Button) findViewById(R.id.b_save_register);
        b_save.setOnClickListener(new OnClickListener()
        {

            public void onClick(View v)
            {
                //TODO
            	saveRegister();
            }

        });
    }
    
    private void saveRegister(){
    	//TODO
    	Item i = new Item();
    	//i.setId(id);
    	i.setName(e_itemName.getText().toString());

    	ItemService service = new ItemService();
    	service.register(i);

		// back to main activity
		Intent intent = new Intent();
        intent.setClass(RegisterActivity.this, MainActivity.class);
        startActivity(intent);
        RegisterActivity.this.finish();
    }
    
    /**
     * Reaction on result of QR Barcode Scanner
     */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode) {
			case IntentIntegrator.REQUEST_CODE: {
					if (resultCode != RESULT_CANCELED) {
						IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
						if (scanResult != null) {
							String upc = scanResult.getContents();
							e_itemID.setText(upc);
						}
					}
					break;
			}
		}
	}
    
}
